using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyNightBattle : EnemyState
{
    protected Transform player;
    protected Enemy_Night enemy;
    private int moveDir;
    public EnemyNightBattle(Enemy _enemybase, EnemyStateMachine _stateMachine, string _animBoolName,Enemy_Night _enemy) : base(_enemybase, _stateMachine, _animBoolName)
    {
        this.enemy = _enemy;
    }
    public override void Enter()
    {
        base.Enter();
        player = PlayerManger.instance.player.transform;
        if (player.GetComponent<PlayerStats>().isDead)
        {
            stateMachine.ChangeState(enemy.nightMove);
        }
    }

    public override void Exit()
    {
        base.Exit();
    }

    public override void Update()
    {
        base.Update();
        if (enemy.IsPlayerDected())
        {
            if (enemy.IsPlayerDected().distance < enemy.attactDistance)
            {
                if (CanAttact() && !player.GetComponent<PlayerStats>().isDead)
                {
                    stateMachine.ChangeState(enemy.nightAttack);
                    return;
                }
            }
        }
        if (player.position.x > enemy.transform.position.x)
        {
            moveDir = 1;
        }
        else if (player.position.x <= enemy.transform.position.x)
        {
            moveDir = -1;
        }
        enemy.SetVelocity(moveDir * enemy.moveSpeed, enemy.rb.velocity.y);

    }
    private bool CanAttact()
    {
        if (Time.time > enemy.lastTimeAttacted + enemy.attactDown)
        {
            enemy.lastTimeAttacted = Time.time;
            return true;
        }
        return false;
    }
}
